package com.tbit.uqbike.client.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tbit.common.config.OssDefaultConfig;
import com.tbit.common.enums.FeeTypeEnum;
import com.tbit.uqbike.client.constant.*;
import com.tbit.uqbike.client.dao.core.*;
import com.tbit.uqbike.client.pojo.*;
import com.tbit.uqbike.client.pojo.newEntity.*;
import com.tbit.uqbike.client.pojo.vo.OutAgeConfig;
import com.tbit.uqbike.client.remote.zk.ZkPayConfig;
import com.tbit.uqbike.client.service.*;
import com.tbit.uqbike.client.util.DateTimeUtil;
import com.tbit.uqbike.client.util.GeoUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 15:38 2020/8/3
 */
@Service
public class CacheServiceImpl implements CacheService {
    @Autowired
    private GeoDao geoDao;
    @Autowired
    private AdAccountFeeDao adAccountFeeDao;
    @Autowired
    private AccountConfigDao accountConfigDao;
    @Autowired
    private WeixinPayConfigDao weixinPayConfigDao;
    @Autowired
    private MpBrandConfigDao mpBrandConfigDao;
    @Autowired
    private SmsAuthConfigDao smsAuthConfigDao;
    @Autowired
    private RedisService redisService;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private MachineBorrowDao machineBorrowDao;
    @Autowired
    private MachineDao machineDao;
    @Autowired
    private RideLogService rideLogService;
    @Autowired
    private AccountConfigExtDao accountConfigExtDao;
    @Autowired
    private ZfbPayConfigDao zfbPayConfigDao;
    @Autowired
    private MachineFeeService machineFeeService;
    @Autowired
    private BrandRemoteConfigDao brandRemoteConfigDao;
    @Autowired
    private ParkFeatureDao parkFeatureDao;
    @Autowired
    private AdaPayConfigDao adaPayConfigDao;
    @Autowired
    private MachineLoseDao machineLoseDao;
    @Autowired
    private OssConfigDao ossConfigDao;
    @Autowired
    private UserRidingScoreConfigDao userRidingScoreConfigDao;
    @Autowired
    private OssDefaultConfig ossDefaultConfig;
    @Resource
    private ForbiddenDao forbiddenDao;
    @Resource
    private ProhibitAreaDao prohibitAreaDao;
    @Resource
    private OutAgeDao outAgeDao;
    @Resource
    private AdAccountFeeService accountFeeService;
    @Resource
    private MachineFunctionDao machineFunctionDao;


    @Override
    public List<Geo> getGeo(Integer accountId) {
        List<Geo> geos;
        /**查看是否redis有缓存*/
        String value = redisService.get(RedisConstant.GEO_KEY_PREFIX + accountId);
        /**没有从数据库获取，然后在存入redis*/
        if (value == null) {
            //LogUtil.info("数据库获取围栏信息"+accountId);
            geos = Optional.ofNullable(geoDao.getByAccountId(accountId)).orElse(new ArrayList<>());
            if (geos.size() > 0) {
                /**经纬度提前转化为集合*/
                for (Geo geo : geos) {
                    List<Point> points = GeoUtil.getPoints(geo.getPoints());
                    geo.setPointList(points);
                }
                /**放入redis,有效期5分钟*/
                redisService.add(RedisConstant.GEO_KEY_PREFIX + accountId, JSON.toJSONString(geos), 300);
            }
        } else {
            geos = JSON.parseArray(value, Geo.class);
        }
        return geos;
    }

    @Override
    public AdAccountFee getAdAccountFee(Integer accountId, Integer machineId) {
        AdAccountFee adAccountFee;
        String value = redisService.get(RedisConstant.FEERULE_KEY_PREFIX + accountId + machineId);
        if (value == null) {
            // 根据骑行订单的开始时间 ， 没查到骑行订单则根据当前是服务器时间
            MachineBorrow machineBorrow = machineBorrowDao.getByMachineId(machineId);
            String startTime = Objects.isNull(machineBorrow) ? DateTimeUtil.getNowTime() : DateUtil.formatDateTime(machineBorrow.getCreateTime());
            adAccountFee = accountFeeService.getFee(accountId,machineId, startTime);
            if (adAccountFee != null) {
                /**放入redis,有效期5分钟*/
                redisService.add(RedisConstant.FEERULE_KEY_PREFIX + accountId + machineId, JSON.toJSONString(adAccountFee), 300);
            }
        } else {
            adAccountFee = JSON.parseObject(value, AdAccountFee.class);
        }
        return adAccountFee;
    }

    @Override
    public AccountConfig getConfig(Integer accountId) {
        AccountConfig accountConfig;
        String value = redisService.get(RedisConstant.CONFIG_KEY_PREFIX + accountId);
        if (value == null) {
            accountConfig = accountConfigDao.getByAccountId(accountId);
            if (accountConfig != null) {
                /**放入redis,有效期5分钟*/
                redisService.add(RedisConstant.CONFIG_KEY_PREFIX + accountId, JSON.toJSONString(accountConfig), 300);
            }
        } else {
            accountConfig = JSON.parseObject(value, AccountConfig.class);
        }
        return accountConfig;
    }

    @Override
    public WeixinPayConfig getWxPay(Integer accountId, Integer type) {
        WeixinPayConfig weixinPayConfig;
        String value = redisService.get(RedisConstant.WXPAY_KEY_PREFIX + accountId + "." + type);
        if (value == null) {
            Map<String, Object> map = new HashMap<>();
            map.put("accountId", accountId);
            map.put("type", type);
            weixinPayConfig = weixinPayConfigDao.getByAccountId(map);
            if (weixinPayConfig != null) {
                /**放入redis,有效期5分钟*/
                redisService.add(RedisConstant.WXPAY_KEY_PREFIX + accountId + "." + type, JSON.toJSONString(weixinPayConfig), 300);
            }
        } else {
            weixinPayConfig = JSON.parseObject(value, WeixinPayConfig.class);
        }
        return weixinPayConfig;
    }

    @Override
    public List<TimeFee> getTimeFeeList(Integer accountId) {
        String value = redisService.get(RedisConstant.TIME_FEE_LIST_KEY_PREFIX + accountId);
        List<TimeFee> timeFeeList;
        if (value == null) {
            timeFeeList = adAccountFeeDao.getTimeFeeByType(accountId);
            /**放入redis,有效期5分钟*/
            redisService.add(RedisConstant.TIME_FEE_LIST_KEY_PREFIX + accountId, JSON.toJSONString(timeFeeList), 300);
        } else {
            timeFeeList = JSON.parseArray(value, TimeFee.class);
        }
        return timeFeeList;
    }

    @Override
    public ZfbPayConfig getZfbPay(Integer accountId, Integer type) {
        ZfbPayConfig zfbPayConfig;
        String value = redisService.get(RedisConstant.ZFBPAY_KEY_PREFIX + accountId + "." + type);
        if (value == null) {
            Map<String, Object> map = new HashMap<>();
            map.put("accountId", accountId);
            map.put("type", type);
            zfbPayConfig = zfbPayConfigDao.getByAccountId(map);
            if (zfbPayConfig != null) {
                /**放入redis,有效期5分钟*/
                redisService.add(RedisConstant.ZFBPAY_KEY_PREFIX + accountId + "." + type, JSON.toJSONString(zfbPayConfig), 300);
            }
        } else {
            zfbPayConfig = JSON.parseObject(value, ZfbPayConfig.class);
        }
        return zfbPayConfig;
    }

    @Override
    public MpBrandConfig getMpConfig(Integer accountId) {
        MpBrandConfig mpBrandConfig;
        String value = redisService.get(RedisConstant.MPCONFIG_KEY_PREFIX + accountId);

        if (value == null) {
            // LogUtil.info("数据库获取小程序配置信息"+accountId);
            mpBrandConfig = mpBrandConfigDao.getByAccountId(accountId);
            if (mpBrandConfig != null) {
                /**放入redis,有效期5分钟*/
                redisService.add(RedisConstant.MPCONFIG_KEY_PREFIX + accountId, JSON.toJSONString(mpBrandConfig), 300);
            }
        } else {
            mpBrandConfig = JSON.parseObject(value, MpBrandConfig.class);
        }
        return mpBrandConfig;
    }

    @Override
    public SmsAuthConfig getSmsConfig(Integer accountId) {
        SmsAuthConfig smsAuthConfig;
        String value = redisService.get(RedisConstant.SMSCONFIG_KEY_PREFIX + accountId);

        if (value == null) {
            // LogUtil.info("数据库获取短信配置信息"+accountId);
            smsAuthConfig = smsAuthConfigDao.getByAccountId(accountId);
            if (smsAuthConfig != null) {
                /**放入redis,有效期5分钟*/
                redisService.add(RedisConstant.SMSCONFIG_KEY_PREFIX + accountId, JSON.toJSONString(smsAuthConfig), 300);
            }
        } else {
            smsAuthConfig = JSON.parseObject(value, SmsAuthConfig.class);
        }
        return smsAuthConfig;
    }

    @Override
    public Account getAccount(Integer accountId) {
        Account account = new Account();
        String value = redisService.get(RedisConstant.ACCOUNT_KEY_PREFIX + accountId);
        if (value == null) {
            List<Account> accounts = accountDao.getAll();
            Map<Integer, Account> map = new HashMap<>();
            /**区域运营商集合*/
            List<Account> agents = new ArrayList<>();
            if (accounts.size() > 0) {
                for (Account a : accounts) {
                    if (a.getType().equals(UserConstant.USER_AGENT)) {
                        agents.add(a);
                    }
                    if (a.getAccountId().equals(accountId)) {
                        account = a;
                    }
                    map.put(accountId, account);
                    redisService.add(RedisConstant.ACCOUNT_KEY_PREFIX + a.getAccountId(), JSON.toJSONString(a), 300);
                }
                /**缓存品牌配置*/
                if (agents.size() > 0) {
                    /**迭代数据组装*/
                    for (Account a : agents) {
                        /**查询对应加盟商*/
                        Account france = map.get(a.getParentId());
                        if (france != null) {
                            /**查询对应品牌*/
                            Account brand = map.get(france.getParentId());
                            /**查询内存是否存在绑定关系*/
                            List<Integer> list = SystemData.brandAgentMap.get(brand.getAccountId());
                            if (list == null) {
                                list = new ArrayList<>();
                            }
                            list.add(a.getAccountId());
                            SystemData.brandAgentMap.put(brand.getAccountId(), list);
                        }
                    }

                }
            }
        } else {
            account = JSON.parseObject(value, Account.class);
        }
        return account;
    }

    @Override
    public RideLog getBorrowInfo(Integer userId, Integer machineId) {
        RideLog rideLog = null;
        if (userId != null) {
            String tmp = redisService.get(RedisConstant.USER_RIDELOG + userId);
            if (tmp != null) {
                rideLog = JSON.parseObject(tmp, RideLog.class);
            }
        }
        if (machineId != null) {
            String tmp = redisService.get(RedisConstant.MACHINE_RIDELOG + machineId);
            if (tmp != null) {
                rideLog = JSON.parseObject(tmp, RideLog.class);
            }
        }
        /**缓存不存在，从数据库查下*/
        if (rideLog == null) {
            if (userId != null) {
                rideLog = rideLogService.getByUserIdNotFinish(userId);
            } else if (machineId != null) {
                MachineBorrow machineBorrow = machineBorrowDao.getByMachineId(machineId);
                if (machineBorrow != null) {
                    rideLog = rideLogService.getByOrderNO(machineBorrow.getOrderNO());
                }
            }
        }
        return rideLog;
    }

    @Override
    public Machine getMachineInfo(Integer machineId, String userCode, String machineNO) {
        Machine machine = null;
        if (machineId != null) {
            machine = SystemData.machineInfoMap.get("machineId." + machineId);
        }
        if (userCode != null) {
            machine = SystemData.machineInfoMap.get("userCode." + userCode);
        }
        if (machineNO != null) {
            machine = SystemData.machineInfoMap.get("machineNO." + machineNO);
        }
        /**缓存不存在，从数据库查下*/
        if (machine == null) {
            if (machineId != null) {
                machine = machineDao.getByMachineId(machineId);
            }
            if (userCode != null) {
                machine = machineDao.getByUserCode(userCode);
            }
            if (machineNO != null) {
                machine = machineDao.getByMachineNO(machineNO);
            }
        }
        return machine;
    }

    @Override
    public List<AccountConfigExt> getAccountConfigExt(Integer accountId) {
        List<AccountConfigExt> accountConfigExt;
        String value = redisService.get(RedisConstant.ACCOUNTCONFIG_KEY_PREFIX + accountId);
        if (value == null) {
            accountConfigExt = accountConfigExtDao.getByAccountId(accountId);
            if (accountConfigExt != null) {
                /**放入redis,有效期5分钟*/
                redisService.add(RedisConstant.ACCOUNTCONFIG_KEY_PREFIX + accountId, JSON.toJSONString(accountConfigExt), 300);
            }
        } else {
            accountConfigExt = JSON.parseArray(value, AccountConfigExt.class);
        }
        if (CollectionUtils.isEmpty(accountConfigExt)){
            return accountConfigExt;
        }
        //先去除掉老的在线客服url
        accountConfigExt = accountConfigExt.stream().filter(accountConfigExt1 -> {
            return !Objects.equals(accountConfigExt1.getParamKey(),AccountConfigExtConstant.ONLINE_CUSTOMER_URL_KEY);
        }).collect(Collectors.toList());
        AccountConfigExt onlineServiceUrlConfig = accountConfigExt.stream().filter(accountConfigExt1 -> {
            return Objects.equals(accountConfigExt1.getParamKey(), AccountConfigExtConstant.ONLINE_SERVICE_URL_KEY);
        }).findFirst().orElse(null);
        if (Objects.nonNull(onlineServiceUrlConfig)){
            //把新的在线客服url放到老的里面
            accountConfigExt.add(new AccountConfigExt(onlineServiceUrlConfig.getAccountId(),
                    AccountConfigExtConstant.ONLINE_CUSTOMER_URL_KEY,onlineServiceUrlConfig.getParamValue(),onlineServiceUrlConfig.getUpdateTime(),onlineServiceUrlConfig.getRemark()));
        }
        return accountConfigExt;
    }

    @Override
    public AccountConfigExt selectByAccountIdAndKey(Integer accountId, String configKey) {
        if (Objects.isNull(accountId) || StringUtils.isBlank(configKey)) {
            return null;
        }
        return accountConfigExtDao.selectByAccountIdAndKey(accountId, configKey);
    }


    @Override
    public BrandRemoteConfig getBrandRemoteConfig(Integer accountId, Integer configType) {
        BrandRemoteConfig brandRemoteConfig = null;
        String value = redisService.get(RedisConstant.BRANDREMOTECONFIG_KEY_PREFIX + accountId + "." + configType);
        if (value == null) {
            BrandRemoteConfig brandRemoteConfigDB = new BrandRemoteConfig();
            brandRemoteConfigDB.setAccountId(accountId);
            brandRemoteConfigDB.setConfigType(configType);
            brandRemoteConfig = brandRemoteConfigDao.getByAccountIdAndType(brandRemoteConfigDB);
            if (brandRemoteConfig != null) {
                /**放入redis,有效期5分钟*/
                redisService.add(RedisConstant.BRANDREMOTECONFIG_KEY_PREFIX + accountId + "." + configType, JSON.toJSONString(brandRemoteConfig), 300);
            }
        } else {
            brandRemoteConfig = JSON.parseObject(value, BrandRemoteConfig.class);
        }
        return brandRemoteConfig;
    }

    @Override
    public JSONObject getBrandJsonConfig(Integer accountId, Integer configType) {
        BrandRemoteConfig brandRemoteConfig = getBrandRemoteConfig(accountId, configType);
        JSONObject jsonObject = new JSONObject();
        if (brandRemoteConfig != null) {
            String key = brandRemoteConfig.getParamKV();
            String[] strings = key.split("&");
            for (String s : strings) {
                jsonObject.put(s.substring(0, s.indexOf("=")), s.substring(s.indexOf("=") + 1));
            }
        }
        return jsonObject;
    }

    @Override
    public String getBrandRemoteConfigValue(Integer accountId, Integer configType) {
        BrandRemoteConfig brandRemoteConfig = getBrandRemoteConfig(accountId, configType);
        if (brandRemoteConfig != null) {
            return brandRemoteConfig.getParamKV().substring(brandRemoteConfig.getParamKV().indexOf("=") + 1);
        }
        return null;
    }

    @Override
    public List<BrandRemoteConfig> getBrandConfig(Integer accountId) {
        List<BrandRemoteConfig> brandRemoteConfigs;
        String value = redisService.get(RedisConstant.BRANDREMOTECONFIG_KEY_PREFIX + accountId);
        if (value == null) {
            brandRemoteConfigs = brandRemoteConfigDao.getByAccountId(accountId);
            if (!brandRemoteConfigs.isEmpty()) {
                redisService.add(RedisConstant.BRANDREMOTECONFIG_KEY_PREFIX + accountId, JSON.toJSONString(brandRemoteConfigs), 300);
            }
        } else {
            brandRemoteConfigs = JSON.parseArray(value, BrandRemoteConfig.class);
        }
        //运营区外禁止实名认证给默认值
        brandRemoteConfigs = getBrandRemoteConfigs(accountId, brandRemoteConfigs,BrandConstant.DISABLE_VERIFY_OUTSIDE,"disableVerifyOutsideOps=0");
        return brandRemoteConfigs;
    }

    /**
     * 补全默认配置
     * @param type               当前需要判断补全的类型
     * @param paramKV            默认值
     * @param accountId          品牌id
     * @param brandRemoteConfigs
     */
    private List<BrandRemoteConfig> getBrandRemoteConfigs(Integer accountId, List<BrandRemoteConfig> brandRemoteConfigs, Integer type, String paramKV) {
        List<Integer> configList = new ArrayList<>();
        if (brandRemoteConfigs != null) {
            configList = brandRemoteConfigs.stream().map(BrandRemoteConfig::getConfigType).collect(Collectors.toList());
        }
        // 为空，或者不包含该type设置默认值
        if(configList.isEmpty() ||!configList.contains(type)){
            // 防空指针
            if (brandRemoteConfigs == null) {
                brandRemoteConfigs =new ArrayList<>();
            }
            BrandRemoteConfig brandRemoteConfig = new BrandRemoteConfig();
            brandRemoteConfig.setAccountId(accountId);
            brandRemoteConfig.setConfigType(type);
            brandRemoteConfig.setParamKV(paramKV);
            brandRemoteConfigs.add(brandRemoteConfig);
        }
        return brandRemoteConfigs;
    }

    @Override
    public AccountConfigExt getAccountConfigExtByKey(Integer accountId, String key) {
        List<AccountConfigExt> accountConfigExts = getAccountConfigExt(accountId);
        if (!accountConfigExts.isEmpty()) {
            for (AccountConfigExt a : accountConfigExts) {
                if (a.getParamKey().equals(key)) {
                    return a;
                }
            }
        }
        return null;
    }

    @Override
    public ParkFeature getParkFeature(Integer parkPointId, Integer type) {
        ParkFeature parkFeature;
        String value = redisService.get(RedisConstant.PARKFEATURE_PREFIX + parkPointId + "." + type);
        if (value == null) {
            ParkFeature parkFeatureDB = new ParkFeature();
            parkFeatureDB.setParkPointId(parkPointId);
            parkFeatureDB.setFeatureType(type);
            parkFeature = parkFeatureDao.getByParkPointId(parkFeatureDB);
            if (parkFeature != null) {
                /**放入redis,有效期5分钟*/
                redisService.add(RedisConstant.ACCOUNTCONFIG_KEY_PREFIX + parkPointId + "." + type, JSON.toJSONString(parkFeature), 300);
            }
        } else {
            parkFeature = JSON.parseObject(value, ParkFeature.class);
        }
        return parkFeature;
    }

    @Override
    public AdaPayConfig getAdaPayConfig(Integer accountId, String pay_channel) {
        AdaPayConfig adaPayConfig = null;
        String value = redisService.get(RedisConstant.ADAPAY_PREFIX + accountId + "." + pay_channel);
        if (value == null) {
            Map<String, Object> map = new HashMap<>();
            map.put("accountId", accountId);
            map.put("pay_channel", pay_channel);
            adaPayConfig = adaPayConfigDao.getByAccountId(map);
            if (adaPayConfig != null) {
                /**放入redis,有效期5分钟*/
                redisService.add(RedisConstant.ADAPAY_PREFIX + accountId + "." + pay_channel, JSON.toJSONString(adaPayConfig), 300);
            }
        } else {
            adaPayConfig = JSON.parseObject(value, AdaPayConfig.class);
        }
        return adaPayConfig;
    }

    /**
     * 从缓存查询对应运营区域的丢失车辆列表
     *
     * @param accountId 运营区域id
     * @return
     */
    @Override
    public List<MachineLose> getMachineLose(Integer accountId) {
        List<MachineLose> machineLoseList;
        String value = redisService.get(RedisConstant.MACHINE_LOSE + accountId);
        if (value == null) {
            machineLoseList = machineLoseDao.getAll(accountId);
            redisService.add(RedisConstant.MACHINE_LOSE + accountId, JSON.toJSONString(machineLoseList), 60 * 60);
        } else {
            machineLoseList = JSON.parseArray(value, MachineLose.class);
        }
        return machineLoseList;
    }

    @Override
    public ZkPayConfig getZkPayConfig(Integer accountId) {
        ZkPayConfig zkPayConfig = null;

        return zkPayConfig;
    }

    @Override
    public ZkPayConfig getZkPayConfigByMgrId(Long mgrId) {
        ZkPayConfig zkPayConfig = null;

        return zkPayConfig;
    }

    @Override
    public OssConfig getOssConfig(Integer accountId, String bucketName) {
        OssConfig ossConfig;
        String value = redisService.get(RedisConstant.OSS_CONFIG_PREFIX + "." + accountId);
        if (value == null) {
            ossConfig = ossConfigDao.getByAccountId(accountId);
            if (ossConfig == null) {
                ossConfig = new OssConfig(ossDefaultConfig.getEndpoint(),
                        ossDefaultConfig.getAccessKeyId(),
                        ossDefaultConfig.getAccessKeySecret(),
                        bucketName);
            }
            /**放入redis,有效期5分钟*/
            redisService.add(RedisConstant.OSS_CONFIG_PREFIX + "." + accountId, JSON.toJSONString(ossConfig), 300);
        } else {
            ossConfig = JSON.parseObject(value, OssConfig.class);
        }
        return ossConfig;
    }

    @Override
    public OssConfig getOssConfigFaceLog(Integer accountId, String bucketName) {
        OssConfig ossConfig;
        String key = RedisConstant.OSS_CONFIG_PREFIX + "." + accountId+"."+bucketName;
        String value = redisService.get(key);
        if (value == null) {
            ossConfig = ossConfigDao.getByAccountId(accountId);
            if (ossConfig == null) {
                ossConfig = new OssConfig(ossDefaultConfig.getEndpoint(),
                        ossDefaultConfig.getAccessKeyId(),
                        ossDefaultConfig.getAccessKeySecret(),
                        bucketName);
            }
            /**放入redis,有效期5分钟*/
            redisService.add(key, JSON.toJSONString(ossConfig), 300);
        } else {
            ossConfig = JSON.parseObject(value, OssConfig.class);
        }
        return ossConfig;
    }

    @Override
    public WeixinPayConfig getByPartnerAndAppId(String partner,String appId, Boolean isCache) {
        WeixinPayConfig weixinPayConfig;
        if (isCache){
            String key = RedisConstant.WXPAY_KEY_PREFIX + partner + "." + appId;
            String value = redisService.get(key);
            if (value == null) {
                weixinPayConfig = weixinPayConfigDao.getByPartnerAndAppId(partner,appId);
                if (weixinPayConfig != null) {
                    /**放入redis,有效期5分钟*/
                    redisService.add(key, JSON.toJSONString(weixinPayConfig), 300);
                }
            } else {
                weixinPayConfig = JSON.parseObject(value, WeixinPayConfig.class);
            }
        }else {
            weixinPayConfig = weixinPayConfigDao.getByPartnerAndAppId(partner,appId);
        }
        return weixinPayConfig;
    }

    @Override
    public UserRidingScoreConfig getUserRidingScoreConfig(Integer brandId) {
        UserRidingScoreConfig userRidingScoreConfig;
        String value = redisService.get(RedisConstant.USER_RIDING_SCORE_CONFIG + brandId);
        if (value != null) {
            userRidingScoreConfig = JSON.parseObject(value, UserRidingScoreConfig.class);
        } else {
            userRidingScoreConfig = userRidingScoreConfigDao.getByBrandId(brandId);
            if (userRidingScoreConfig == null) {
                userRidingScoreConfig = new UserRidingScoreConfig(brandId, 0, 0, 0, 0);
            }
            //缓存五分钟
            redisService.add(RedisConstant.USER_RIDING_SCORE_CONFIG + brandId, JSON.toJSONString(userRidingScoreConfig), 300);
        }
        return userRidingScoreConfig;
    }

    @Override
    public Integer getBrandId(Integer accountId) {
        Integer brandId = -1;
        String s = redisService.get(RedisConstant.ACCOUNT_BRAND_PREFIX + accountId);
        if (s == null) {
            brandId = accountDao.getBrandId(accountId);
            redisService.add(RedisConstant.ACCOUNT_BRAND_PREFIX + accountId, brandId == null ? "-1" : brandId.toString(), 3600);
        } else {
            brandId = Integer.parseInt(s);
        }
        return brandId;
    }

    @Override
    public Boolean configValid(Integer accountId, String configKey) {
        List<AccountConfigExt> accountConfigExtList = getAccountConfigExt(accountId);
        return accountConfigExtList.stream().anyMatch(x -> Objects.equals(x.getParamKey(),configKey) && Objects.equals("1",x.getParamValue()));
    }

    @Override
    public List<Forbidden> getForbidden(Integer accountId) {
        if (Objects.isNull(accountId)){
            return new ArrayList<>();
        }
        List<Forbidden> forbiddenList = new ArrayList<>();
        String s = redisService.get(RedisConstant.ACCOUNT_FORBIDDEN + accountId);
        if (StrUtil.isEmpty(s)){
            forbiddenList = forbiddenDao.getByAccountId(accountId);
            //缓存30分钟
            redisService.add(RedisConstant.ACCOUNT_FORBIDDEN + accountId, JSON.toJSONString(forbiddenList), 60 * 10);
        }else {
            forbiddenList = JSON.parseArray(s, Forbidden.class);
        }
        return forbiddenList;
    }

    @Override
    public List<ProhibitArea> getByArea(Integer accountId) {
        if (Objects.isNull(accountId)){
            return new ArrayList<>();
        }
        List<ProhibitArea> forbiddenList = new ArrayList<>();
        String s = redisService.get(RedisConstant.ACCOUNT_PROHIBITAREA + accountId);

        if (StrUtil.isEmpty(s)){
            forbiddenList = prohibitAreaDao.getByAccountId(accountId);
            //缓存30分钟
            redisService.add(RedisConstant.ACCOUNT_PROHIBITAREA + accountId, JSON.toJSONString(forbiddenList), 60 * 10);
        }else {
            forbiddenList = JSON.parseArray(s, ProhibitArea.class);
        }
        return forbiddenList;
    }


    /**
     * 获取停运配置
     * @param brandId 品牌id
     * @param accountId 区域id
     * @return OutAgeConfig
     */
    @Override
    public OutAgeConfig getOutAgeConfig(Integer brandId, Integer accountId) {
        OutAgeConfig outAgeConfig;
        if (Objects.isNull(brandId) && Objects.isNull(accountId)) {
            return null;
        }

        String key = Objects.nonNull(brandId)
                ? BrandConstant.SERVICE_SWITCH + "_" + brandId
                : BrandConstant.SERVICE_SWITCH + "_" + accountId;

        String cachedConfig = redisService.get(key);
        if (StrUtil.isEmpty(cachedConfig)) {
            outAgeConfig = outAgeDao.selectOutAgeConfig(
                    Objects.nonNull(brandId) ? brandId : null,
                    Objects.nonNull(accountId) ? accountId : null
            );
            redisService.add(key, JSON.toJSONString(outAgeConfig), 60 * 5);
        } else {
            outAgeConfig = JSON.parseObject(cachedConfig, OutAgeConfig.class);
        }

        return outAgeConfig;
    }

    /**
     * 根据key名获取区域的拓展信息
     *
     * @param accountId 区域id
     * @return
     */
    @Override
    public List<AccountConfigExt> getAccountConfigExtByKeys(Integer accountId, List<String> keys) {
        List<AccountConfigExt> accountConfigExts = getAccountConfigExt(accountId);
        if (!accountConfigExts.isEmpty()) {
            return accountConfigExts.stream().filter(accountConfigExt -> {
                return keys.contains(accountConfigExt.getParamKey());
            }).collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public FeeTypeEnum queryFeeType(Integer accountId) {
        String value = redisService.get(RedisConstant.TIME_FEE_TYPE_KEY_PREFIX + accountId);
        if (value == null){
            FeeTypeEnum feeTypeEnum = accountFeeService.queryFeeType(accountId);
            redisService.add(RedisConstant.TIME_FEE_TYPE_KEY_PREFIX + accountId, feeTypeEnum.toString(), 60 * 5);
            return feeTypeEnum;
        }
        return JSON.parseObject(value, FeeTypeEnum.class);
    }

    @Override
    public List<MachineFunction> getMachineFunction(Integer machineId) {
        String value = redisService.get(RedisConstant.getMachineFunctionKey(machineId));
        if (value == null){
            List<MachineFunction> functions = machineFunctionDao.getAll(machineId);
            redisService.add(RedisConstant.getMachineFunctionKey(machineId), JSON.toJSONString(functions), 60 * 5);
            return functions;
        }
        return JSON.parseArray(value, MachineFunction.class);
    }


}
